<template>
    <orh-trees>
        <Tree :data="trees" :load-data="loadData" slot="trees" @on-select-change="clicktreemenu"></Tree>
		<current-term @on-formstate="formState" style="width:200px" slot="option"></current-term>
		<Button type="primary" slot="option" style="margin-left: 10px;" :loading="butloading" @click="exportAll" v-button="'mydepartall_export'" size="small">导出我的部门请假</Button>
        <Button type="primary" slot="option" style="margin-left: 10px;" @click="exportSameDay" v-button="'mydepartsame_export'" size="small">选择导出数据</Button>
        <Modal v-model="lookupState" slot="others">
			<p slot="header"><span>请选择时间段</span></p>
			<div style="text-align:center">
				<DatePicker type="daterange" v-if="lookupState" @on-change="seltime" placeholder="选择时间段" style="width: 200px"></DatePicker>
			</div>
			<div slot="footer">
				<Button type="primary" :loading="loading" @click="lookupSubmit">确定</Button>
				<Button @click="lookupState=false">取消</Button>
			</div>
		</Modal>
		<orh-sub-table pages :pageDate="pageDate" @on-page-change="queryData" slot="content">
			<Table slot="table" :columns="columns1" :data="HonorList" :loading="tableload"></Table>
		</orh-sub-table>
		<orh-sidebar v-model="show" title="查看详情" scale slot="others">
			<my-department-detail :datas="datas"></my-department-detail>
		</orh-sidebar>
    </orh-trees>
</template>
<script>
export default {
	components: {
		CurrentTerm: resolve=> require(['&/Authpub/CurrentTerm.vue'],resolve),
		MyDepartmentDetail: resolve => {require(['./MyDepartmentDetail.vue'], resolve);},
	},
  data() {
    return {
		datas:null,
		loading:false,
		lookupState:false,
		butloading:false,
		pageDate: {
			total: 0,
			current_page: 1,
			per_page: 1
		},
		addval:{
			department_fid_id:null,
			department_id:null,
		},
		start_time:'',
		end_time:'',
		allid:null,
		title:null,
		show: false,
      	trees: [],
		HonorList:[],
		tableload:false,
		url:'api/leave/v1/leave_statistics/teacher_leave_statistics?department_id=',
		departmentId:null,
		columns1: [
			{
				title: '教师姓名',
				render: (h, params) => {
					return h('div', params.row.teacher!=null?params.row.teacher.teacher_name.zh:'');
				}
			},
			{
				title: '请假种类',
				render: (h, params) => {
					return h('div', params.row.teacher_leave_kinds.length != 0?params.row.teacher_leave_kinds[0].dictionary_name.zh:'');
				}
			},
			{
				title: '请假开始时间',
				align: 'center',
				key:'start_time',
			},
			{
				title: '请假结束时间',
				align: 'center',
				key:'end_time',
			},
			{
				title: '是否调课',
				align: 'center',
				render: (h, params) => {
					return h('div', [
						h('div',params.row.whether_class == '1'?'是':'否')
					]);
				}
			},
			{
				title: '审核状态',
				align: 'center',
				render: (h, params) => {
				if(params.row.examine_status == '0'){
					return h('div', [
					h('div',{
						style: {
						color: '#007d7f',
						},
					}, '未审核')
					]);
				}else if(params.row.examine_status == '1'){
					return h('div', [
					h('div',{
						style: {
						color: '#1aad33',
						},
					}, '通过')
					]);
				}else if(params.row.examine_status == '2'){
					return h('div', [
					h('div',{
						style: {
						color: '#f00',
						},
					}, '未通过')
					]);
				}else{
					return h('div', [
					h('div',{
						style: {
						color: '#FFC107',
						},
					}, '审核中')
					]);
				}
				}
			},
			{
				title: '操作',
				key: 'operation',
				align: 'center',
				width: 200,
				render: (h, params) => {
					return h('div', [
						h('Button', {
							props: {
								type: 'success',
								size: 'small'
							},
							style: {
								marginRight: '5px',
							},
							on: {
								click: () => {
								this.look(params.row)
								}
							}
						}, '查看'),
					]);
				}
			}
		],
		};
  	},
  	methods: {
		/************************************************************
		* 选择学期
		************************************************************/
        formState(val){
            if(val!=null){
				this.allid = val
				this.url = `api/leave/v1/leave_statistics/teacher_leave_statistics?semester_id=${val}&department_id=`
				this.queryData(this.departmentId)
            }
		},
		/************************************************************
		* 获取部门
		************************************************************/
		getAllDepart() {
			this.$http.get('api/substance/v1/application/department').then(res=> {
				this.trees = this.process(res.data,'parent')
			}).catch(err => {
				if (err.code && err.code == 422) {
					this.$Message.error(err.message);
				}
			})
    	},
		/************************************************************
		* 处理数据
		************************************************************/
		process(data,type) {
			let arr = [];
			data.forEach((item, index) => {
				arr[index] = {};
				arr[index].title = item.department_name.zh?item.department_name.zh:item.department_name.mn;
				arr[index].id = item.id;
				arr[index].type = type;
				if (item.childs != 0) {
					(arr[index].loading = false),(arr[index].children = []);
				}
			});
			return arr;
		},
		/************************************************************
		* 异步加载二级部门
		************************************************************/
		loadData(item, callback) {
			this.departmenloading=item.id
			this.$http.get(`api/user/v1/get/department/list?pid=${item.id}`).then(val => {
				callback(this.process(val.data,'childs'));
			}).catch(err => this.$Message.error("子部门加载失败！"));
		},
		/************************************************************
		* 点击部门
		************************************************************/
		clicktreemenu(val) {
			if(val[0].type=='parent'){
				this.addval.department_fid_id=val[0].id
				this.addval.department_id=null
				this.departmentId = this.addval.department_fid_id
				this.queryData(this.addval.department_fid_id)
			}
			if(val[0].type=='childs'){
				this.addval.department_fid_id=val[0].id
				if(this.addval.department_fid_id!=null){
					this.departmentId = this.addval.department_fid_id
					this.queryData(this.addval.department_fid_id)
					if(this.addval.department_id!=null){
						this.departmentId = this.addval.department_id
						this.queryData(this.addval.department_id)
					}
				}
			}
		},
		/************************************************************
		* 时间处理
		************************************************************/
		seltime(val){
			this.start_time = val[0]
			this.end_time = val[1]
		},
		/************************************************************
		* 选择时间段导出数据
		************************************************************/
		lookupSubmit(){
			this.lookupState = false
			this.publicexport()
		},
		/************************************************************
		* 查看
		************************************************************/
		look(row){
			this.show = true
			this.datas = row;
		},
		/************************************************************
		* 获取数据
		************************************************************/
		queryData(id){
			this.tableload = true
			this.$http.get(`api/leave/v1/teacher_leave/index?department_id=${id}`).then(val=>{
				this.HonorList = val.data.data
				this.pageDate.total=val.data.total
				this.pageDate.per_page=val.data.per_page
				this.pageDate.current_page=val.data.current_page
				this.tableload = false
			}).catch(err => {
				if (err.code && err.code == 422) {
					this.$Message.error(err.message);
				}
				this.tableload = false
			})
		},
		/************************************************************
		* 导出
		************************************************************/
		exportAll(){
			if(this.allid!=null){
				if(this.departmentId!=null){
					this.butloading=true
					this.$http.post(`api/leave/v1/teacher_leave/department_export?semester_id=${this.allid}&department_id=${this.departmentId}`).then(val => {
						this.butloading=false
						window.open(val.data.url)
					}).catch(err => {
						console.log(err)
					})
				}else{
					this.$Message.info('请选择部门');
				}
			}else{
				this.$Message.info('请选择学期');
			}
		},
		/************************************************************
		* 选择导出数据
		************************************************************/
		exportSameDay(){
			if(this.allid!=null){
				if(this.departmentId!=null){
					this.lookupState = true
					this.publicexport()
				}else{
					this.$Message.info('请选择部门');
				}
			}else{
				this.$Message.info('请选择学期');
			}
		},
		/************************************************************
		* 选择导出数据的公共方法
		************************************************************/
		publicexport(){
			this.loading = true
			if(this.start_time!='' && this.end_time!=''){
				this.$http.post(`api/leave/v1/teacher_leave/department_export?semester_id=${this.allid}&department_id=${this.departmentId}&start_time=${this.start_time}&end_time=${this.end_time}`).then(val => {
					window.open(val.data.url)
					this.start_time=''
					this.end_time=''
					this.loading = false
				}).catch(err => {
					this.loading = false
				})
			}
		}
	},
	created() {
		this.getAllDepart()
	},
}
</script>
